<?php

class Controller extends controllerBase {

	public function index($tag = '')
	{		
		if($tag != '')	//display a specific tag
		{
			$qTag = new Query("SELECT * FROM `tags` WHERE `tag` = ?", $tag);
			$p = $qTag->fetch_row();
			
			//get blogs
			$show .= '<h3> Blog Entries </h3>';
			$query = new Query("SELECT * FROM  `blog_tag_relations` WHERE `tag` = ? ORDER BY `id` DESC", $p[0]);
			
			if($query->num_rows() > 0)
			{
				$blog_tag = blog_blogTag::recordset($query, 'blog_blogTag');
				
				for($i = 0; $i < count($blog_tag); $i++)
				{
					$blog = new blog_blog($blog_tag[$i]->blog);
					
					$show .= '<h2>' . display::link($blog->title, 'blog/' . $blog->url) . '</h2>
				           <h3 class="date">' . format::date($blog->date) . '</h3>
						   ' . format::short($blog->content) . '
						   <p>' . display::link('Read Full Story/Add Comment', 'blog/' . $blog->url) . '</p>';
				}
			}
			else
			{
				$show .= '<p> There are no blog entries associated with this tag.</p>';
			}
			
			
			//get images
			$show .= '<h3> Images </h3>';
			$query = new Query("SELECT * FROM `image_tag_relations` WHERE `tag` = ? ORDER BY `id` DESC", $p[0]);
			
			if($query->num_rows() > 0)
			{
				$img_tag = image_imgTag::RecordSet($query, 'image_imgTag');
				
				for($i = 0; $i < count($img_tag); $i++)
				{
					$image = new image_image($img_tag[$i]->image);
					$show .= '<p><a href="' . ROOT_URL . URL_BASE . 'image/' . $image->id . '"><img src="' . ROOT_URL . 'images/med/' . $image->url . '" alt="' . $image->description . '" /></a></p>';
				}
			}
			else
			{
				$show .= '<p> There are no images associated with this tag.</p>';
			}
			
			$this->title = "Tags :: " . $p[1];
			$this->content = '<h2>' . $p[1] .'<h2
					' . $show;
			
		}
		else	//display all tags
		{
			$query = new Query("SELECT * FROM `tags` ORDER BY `id` DESC");
			
			$tags = tag::RecordSet($query, 'tag');
			
			if(count($tags) > 0)
			{
			
				for($i = 0; $i < count($tags); $i++)
				{
					$show .= '<h3><a href="' . ROOT_URL . URL_BASE . 'tag/' . $tags[$i]->tag . '">' . $tags[$i]->tag . '</a></h3>';
				}
			}
			
			$this->title = "Tags";
			$this->content = $show;
		}
	}
}
?>